#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import json
from flask import Blueprint,request,Response

from tool.common import *
from parser.task_parser import TaskParser
from tool.common import *

task_parser = TaskParser()
task_server = Blueprint("/task",__name__)

#---------------------------------#
#******#   flask-网络端口验证   #******#
#---------------------------------#
@task_server.before_request
def before():
    res_data = before_user(request)
    if res_data:
        return Response(json.dumps(res_data),  mimetype='application/json')


#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 区域任务路径预览
# req: 
# res: 
@task_server.route('/clean_area_pre',methods=['POST'])
def Post_PreviewCleanTaskPath():
    data = request.json
    res_data = task_parser.PreviewCleanTaskPath(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 单个任务新建保存
# req: 
# res: 
@task_server.route('/task_add',methods=['POST'])
def Task_add():
    data = request.json
    res_data = task_parser.Task_add(data)
    return Response(json.dumps(res_data),  mimetype='application/json')


#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 清扫任务删除
# req: 
# res: 
@task_server.route('/task_del',methods=['POST'])
def Post_DeleteCleanTask():
    data = request.json
    res_data = task_parser.Task_delet(data)
    return Response(json.dumps(res_data),  mimetype='application/json')


#---------------------------------#
#******#   POST   #******#
#---------------------------------#
# 清扫任务重命名
# req: 
# res: 
@task_server.route('/task_rename',methods=['POST'])
def Post_RenameCleanTask():
    data = request.json
    res_data = task_parser.Task_rename(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 清扫任务列表查询
# req: None
# res:  
@task_server.route('/cleantask_origin',methods=['POST'])
def Post_ListTaskOrigin():
    data = request.json
    res_data = task_parser.Task_list(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

# #---------------------------------#
#     #******#   POST   #******#
# #---------------------------------#
# # 执行任务队列查询
# # req: {u'map_name': u'\u6d4b\u8bd5'}
# # res: 
# @task_server.route('/task_list',methods=['POST'])
# def Post_ListCleanTask():
#     data = request.json
#     res_data = task_parser.ListCleanTask(data)
#     return Response(json.dumps(res_data),  mimetype='application/json')

# # 清扫任务重新规划
# # 20231201-dw.liu
# @task_server.route('/task_regenerate',methods=['POST'])
# def Post_TaskRegenerate():
#     data = request.json
#     res_data = task_parser.TaskRegenerate(data)
#     return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 定时任务添加
# req:
# res: 
@task_server.route('/timetask_add',methods=['POST'])
def Post_AddTimertask():
    data = request.json
    res_data = task_parser.AddTimertask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')
#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 定时任务信息查询
# req: None
# res: 
@task_server.route('/timetask_info',methods=['POST'])
def Post_ListTimertask():
    data = request.json
    res_data = task_parser.ListTimertask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')
#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 定时任务开关
# req: 
# res: 
@task_server.route('/timetask_cancel',methods=['POST'])
def Post_CancelTimertask():
    data = request.json
    res_data = task_parser.CancelTimertask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 定时任务编辑
# req: {u'enable': 1, u'timetask_list': [u'5\u697c\u6d4b\u8bd5', u'fffff', u'kkkk', u'\u4e2d\u6587abc123456', u'\u5168\u533a\u57df111'], u'start_time': u'07-30',  \
#       u'days': [1, 1, 1, 1, 1, 1, 1], u'map_name': u'\u6d4b\u8bd5', u'timetask_name': u'\u5b9a\u65f6\u4efb\u52a1\u6d4b\u8bd51'}
# res: {'code': 0, 'ret': 'ok'}
@task_server.route('/timetask_modofied',methods=['POST'])
def Post_ModifyTimertask():
    data = request.json
    res_data = task_parser.ModifyTimertask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 定时任务删除
# req: {u'map_name': u'\u6d4b\u8bd5', u'timetask_name': u'\u5b9a\u65f6\u4efb\u52a1\u6d4b\u8bd51'}
# res: {'code': 0, 'ret': 'ok'}
@task_server.route('/timetask_delete',methods=['POST'])
def Post_DeleteTimertask():
    data = request.json
    res_data = task_parser.DeleteTimertask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 定时任务重命名
# req: {u'map_name': u'\u6d4b\u8bd5', u'timetask_newname': u'\u5b9a\u65f6\u4efb\u52a12', u'timetask_oldname': u'\u5b9a\u65f6\u4efb\u52a1\u6d4b\u8bd51'}
# res: {'code': 0, 'ret': 'ok'}
@task_server.route('/timetask_rename',methods=['POST'])
def Post_RenameTimertask():
    data = request.json
    res_data = task_parser.RenameTimertask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')


'''
    在线任务相关
'''

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 清扫任务开始执行
# req: {u'task_name': u'fffff'}
# res:
@task_server.route('/task_start',methods=['POST'])
def Post_StartCleanTask():
    data = request.json
    res_data = task_parser.StartCleanTask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')
#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 执行任务队列查询
# req: {u'map_name': u'\u6d4b\u8bd5'}
# res: 
@task_server.route('/task_list',methods=['POST'])
def Post_ListCleanTask():
    data = request.json
    res_data = task_parser.ListCleanTask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 清扫任务取消单个任务
# req: {u'task_name': u'fffff', u'add_time': u'1609748391'}
# res:
@task_server.route('/task_cancel_one',methods=['POST'])
def Post_CancelOneCleanTask():
    data = request.json
    res_data = task_parser.CancelOneCleanTask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 清扫任务取消所有任务
# req: 
# res:
@task_server.route('/task_cancel',methods=['POST'])
def Post_CancelAllCleanTask():
    res_data = task_parser.CancelAllCleanTask()
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 清扫任务暂停执行
# req: None
# res:
@task_server.route('/task_pause',methods=['POST'])
def Post_PauseCleantask():
    res_data = task_parser.PauseCleanTask()
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 清扫任务恢复执行
# req: None
# res:
@task_server.route('/task_resume',methods=['POST'])
def Post_ResumeCleantask():
    res_data = task_parser.ResumeCleanTask()
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 执行任务队列重新排序
# req: {u'map_name': u'\u6d4b\u8bd5'}
# res: 
@task_server.route('/task_order',methods=['POST'])
def Post_OrderCleanTask():
    data = request.json
    res_data = task_parser.OrderCleanTask(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 示教路径记录开始
# req: None
# res:  
@task_server.route('/teachpath_generate',methods=['POST'])
def Post_StartTeachingPathRecord():
    res_data = task_parser.StartTeachingPathRecord()
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 示教路径记录取消
# req: None
# res:  
@task_server.route('/teachpath_save_cancel',methods=['POST'])
def Post_CancelTeachingPathRecord():
    res_data = task_parser.CancelTeachingPathRecord()
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 示教路径保存
# req: None
# res:  
@task_server.route('/teachpath_save',methods=['POST'])
def Post_SaveTeachingPathRecord():
    data = request.json
    res_data = task_parser.SaveTeachingPathRecord(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
# ft.wang 20240606接手
#---------------------------------#
# 报告清扫任务列表 
@task_server.route('/report_list',methods=['POST'])
def Post_ReportTaskList():
    data = request.json
    res_data = task_parser.ReportTaskList(data)
    return Response(json.dumps(res_data),  mimetype='application/json')


#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 推行任务开始
# req: None
# res:
@task_server.route('/push_task_start',methods=['POST'])
def Post_PushTaskStart():
    res_data = task_parser.PushTaskStart()
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 推行任务取消
# req: None
# res:
@task_server.route('/push_task_canel',methods=['POST'])
def Post_PushTaskCanel():
    res_data = task_parser.PushTaskCanel()
    return Response(json.dumps(res_data),  mimetype='application/json')


#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 推行任务保存
# req: 
# res: 
@task_server.route('/push_task_save',methods=['POST'])
def Post_PushTaskSave():
    data = request.json
    res_data = task_parser.PushTaskSave(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 推行任务路径预览
# req: 
# res: 
@task_server.route('/push_task_pre',methods=['POST'])
def Post_PushTaskPre():
    data = request.json
    res_data = task_parser.PushTaskPre(data)
    return Response(json.dumps(res_data),  mimetype='application/json')


# 清扫任务重新规划
@task_server.route('/task_regenerate',methods=['POST'])
def Post_TaskRegenerate():
    data = request.json
    res_data = task_parser.TaskRegenerate(data)
    return Response(json.dumps(res_data),  mimetype='application/json')
